<template>
  <!--单个列展示-->
  <u-table-column
    :key="'col' + col.key"
    :prop="col.prop"
    :label="col.label"
    :width="col.width"
    :align="col.align || 'right'"
    header-align="center"
  >
    <template slot-scope="scope">
      <span
        :class="col.markStyle ? standard(scope.row[col.prop]) : ''"
      >
        {{
          col.convert
            ? formatCurrency(scope.row[col.prop])
            : col.toPercent
              ? formatPercent(scope.row[col.prop])
              : scope.row[col.prop] || "-"
        }}
      </span>
    </template>
    <!--递归遍历children属性-->
    <template v-if="col.children && col.children.length > 0">
      <virtual-multi-level-colums
        v-for="item in col.children"
        :key="'child-' + item.key"
        :col="item"
      />
    </template>
  </u-table-column>
</template>

<script>
import { formatThousands } from '@/utils/tools';
export default {
  name: 'VirtualMultiLevelColums',
  props: {
    col: {
      type: Object,
      required: true,
      default: () => {}
    }
  },
  computed: {
    // 校验标准
    standard() {
      return function(val) {
        return val >= 0 ? 'standard' : val < 0 ? 'unstandard' : '';
      };
    },
    // 格式化金额公式
    formatCurrency() {
      return function(num) {
        return formatThousands(num);
      };
    },
    formatPercent() {
      return function(point) {
        if (!point) return '-';
        let percent = Number(point * 100).toFixed(1);
        percent += '%';
        return percent;
      };
    }
  }
};
</script>

<style lang="scss">
.title-center {
  text-align: center !important;
}
.el-table__header-wrapper {
  .el-table__header {
    .is-group {
      .el-table_1_column_1 .is-left .is-leaf {
        text-align: center !important;
      }
    }
  }
}
.standard {
  color: #67c23a;
}
.unstandard {
  color: #f56c6c;
}
</style>
